/*     */ package com.zimbra.cs.filter;
/*     */ 
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailboxListener;
/*     */ import com.zimbra.cs.mailbox.MailboxListener.ChangeNotification;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.mailbox.Tag;
/*     */ import com.zimbra.cs.session.PendingModifications;
/*     */ import com.zimbra.cs.session.PendingModifications.Change;
/*     */ import com.zimbra.cs.session.PendingModifications.ModificationKey;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FilterListener
/*     */   extends MailboxListener
/*     */ {
/*  37 */   public static final ImmutableSet<MailboxOperation> EVENTS = ImmutableSet.of(MailboxOperation.MoveItem, MailboxOperation.DeleteItem, MailboxOperation.RenameItem, MailboxOperation.RenameItemPath, MailboxOperation.RenameTag);
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  43 */   public static final ImmutableSet<MailItem.Type> ITEMTYPES = ImmutableSet.of(MailItem.Type.FOLDER, MailItem.Type.MOUNTPOINT, MailItem.Type.TAG);
/*     */   
/*     */ 
/*     */ 
/*     */   public void notify(MailboxListener.ChangeNotification notification)
/*     */   {
/*  49 */     if ((notification.mods.modified != null) && (EVENTS.contains(notification.op))) {
/*  50 */       for (PendingModifications.Change change : notification.mods.modified.values()) {
/*  51 */         if ((change.what instanceof Folder)) {
/*  52 */           if (((change.why & 0x200) != 0) || ((change.why & 0x1000) != 0))
/*     */           {
/*     */ 
/*  55 */             Folder folder = (Folder)change.what;
/*  56 */             Folder oldFolder = (Folder)change.preModifyObj;
/*  57 */             if (oldFolder == null) {
/*  58 */               ZimbraLog.filter.warn("Cannot determine the old folder name for %s.", new Object[] { folder.getName() });
/*     */             }
/*     */             else
/*  61 */               updateFilterRules(notification.mailboxAccount, folder, oldFolder.getPath());
/*  62 */           } } else if ((change.what instanceof Tag))
/*  63 */           if ((change.why & 0x1000) != 0)
/*     */           {
/*     */ 
/*  66 */             Tag tag = (Tag)change.what;
/*  67 */             Tag oldTag = (Tag)change.preModifyObj;
/*  68 */             if (oldTag == null) {
/*  69 */               ZimbraLog.filter.warn("Cannot determine the old tag name for %s.", new Object[] { tag.getName() });
/*     */             }
/*     */             else
/*  72 */               updateFilterRules(notification.mailboxAccount, tag, oldTag.getName());
/*     */           }
/*     */       }
/*     */     }
/*  76 */     if (notification.mods.deleted != null) {
/*  77 */       for (Map.Entry<PendingModifications.ModificationKey, PendingModifications.Change> entry : notification.mods.deleted.entrySet()) {
/*  78 */         MailItem.Type type = (MailItem.Type)((PendingModifications.Change)entry.getValue()).what;
/*  79 */         if ((type == MailItem.Type.FOLDER) || (type == MailItem.Type.MOUNTPOINT)) {
/*  80 */           Folder oldFolder = (Folder)((PendingModifications.Change)entry.getValue()).preModifyObj;
/*  81 */           if (oldFolder == null) {
/*  82 */             ZimbraLog.filter.warn("Cannot determine the old folder name for %s.", new Object[] { entry.getKey() });
/*     */           }
/*     */           else
/*  85 */             updateFilterRules(notification.mailboxAccount, (Folder)null, oldFolder.getPath());
/*  86 */         } else if (type == MailItem.Type.TAG) {
/*  87 */           Tag oldTag = (Tag)((PendingModifications.Change)entry.getValue()).preModifyObj;
/*  88 */           updateFilterRules(notification.mailboxAccount, oldTag);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public Set<MailItem.Type> registerForItemTypes()
/*     */   {
/*  96 */     return ITEMTYPES;
/*     */   }
/*     */   
/*     */   private void updateFilterRules(Account account, Folder folder, String oldPath) {
/*     */     try {
/* 101 */       if ((folder == null) || (folder.inTrash()) || (folder.isHidden())) {
/* 102 */         ZimbraLog.filter.info("Disabling filter rules that reference %s.", new Object[] { oldPath });
/* 103 */         RuleManager.folderDeleted(account, oldPath);
/* 104 */       } else if (!folder.getPath().equals(oldPath)) {
/* 105 */         ZimbraLog.filter.info("Updating filter rules that reference %s.", new Object[] { oldPath });
/* 106 */         RuleManager.folderRenamed(account, oldPath, folder.getPath());
/*     */       }
/*     */     } catch (ServiceException e) {
/* 109 */       ZimbraLog.filter.warn("Unable to update filter rules with new folder path.", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private void updateFilterRules(Account account, Tag tag, String oldName) {
/*     */     try {
/* 115 */       ZimbraLog.filter.info("Updating filter rules that reference %s.", new Object[] { oldName });
/* 116 */       RuleManager.tagRenamed(account, oldName, tag.getName());
/*     */     } catch (ServiceException e) {
/* 118 */       ZimbraLog.filter.warn("Unable to update filter rules with new folder path.", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private void updateFilterRules(Account account, Tag tag) {
/*     */     try {
/* 124 */       ZimbraLog.filter.info("Disabling filter rules that reference %s.", new Object[] { tag.getName() });
/* 125 */       RuleManager.tagDeleted(account, tag.getName());
/*     */     } catch (ServiceException e) {
/* 127 */       ZimbraLog.filter.warn("Unable to update filter rules with new folder path.", e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/FilterListener.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */